package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.AllInstallBean;
import com.wnhz.work.yz.bean.InstallDetailsBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReceiveAllActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_all);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("安装订单");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        initVeiw();
    }

    private RecyclerView title, content;
    private BaseRVAdapter titleAdapter;


    private String[] str = {"全部", "待接单", "待安装", "安装中", "待验收", "验收完成", "整改中"};
    private List<TestBean> statusStr = new ArrayList<>();

    private void initVeiw() {
        for (int i = 0; i < str.length; i++) {
            TestBean test = new TestBean();
            test.setSelect(false);
            if (i == 0) {
                test.setSelect(true);
            }
            test.setNums(str[i]);
            statusStr.add(test);
        }
        upLoad(0);
        title = (RecyclerView) findViewById(R.id.title);
        content = (RecyclerView) findViewById(R.id.content);
        title.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        content.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        titleAdapter = new BaseRVAdapter(this, statusStr) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_receive_title;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.setTextView(R.id.text, statusStr.get(position).getNums());
                if (statusStr.get(position).isSelect()) {
                    holder.getView(R.id.line).setVisibility(View.VISIBLE);
                } else {
                    holder.getView(R.id.line).setVisibility(View.INVISIBLE);
                }
                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (int i = 0; i < statusStr.size(); i++) {
                            if (i == position) {
                                statusStr.get(i).setSelect(true);
                            } else {
                                statusStr.get(i).setSelect(false);
                            }
                        }
                        notifyDataSetChanged();
                        upLoad(position);

                    }
                });
            }


        };
        title.setAdapter(titleAdapter);

    }

    private List<AllInstallBean.InfoEntity> info1 = new ArrayList<>();

    private void upLoad(int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", position);
        showDialog();
        XUtil.Post(Url.DECORATE_ORDERLISTOFINSTALLER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        AllInstallBean bean = gson.fromJson(result, AllInstallBean.class);
                        info1 = bean.getInfo();
                        setData();
                        content.setVisibility(View.VISIBLE);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                        content.setVisibility(View.GONE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private BaseRVAdapter adapter;

    private void setData() {
        adapter = new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_install_fragment;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.setTextView(R.id.price, info1.get(position).getAmount());


                holder.setTextView(R.id.order_num, info1.get(position).getOrder_no());
                holder.setTextView(R.id.creat_time, info1.get(position).getCreate_time());
                //0已取消 1待付款 2待接单 3待安装 4安装中 5待验收 6验收完成 7整改中 8整改完待验收
                switch (Integer.parseInt(info1.get(position).getStatus())) {
                    case 0:
                        holder.setTextView(R.id.status, "已取消");
                        holder.getView(R.id.status0).setVisibility(View.GONE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.quxiao).setVisibility(View.VISIBLE);
                        holder.getView(R.id.gotopay).setVisibility(View.VISIBLE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        break;
                    case 1:
                        holder.setTextView(R.id.status, "待付款");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.GONE);
                        holder.getView(R.id.quxiao).setVisibility(View.VISIBLE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.VISIBLE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        break;
                    case 2:
                        holder.setTextView(R.id.status, "待接单");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.GONE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        break;
                    case 3:
                        holder.setTextView(R.id.status, "待安装");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.GONE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.VISIBLE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);

                        break;
                    case 4:
                        holder.setTextView(R.id.status, "安装中");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.jungong).setVisibility(View.VISIBLE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.setTextView(R.id.status, "待验收");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        break;
                    case 6:
                        holder.setTextView(R.id.status, "验收完成");
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        break;
                    case 7:
                        holder.setTextView(R.id.status, "整改中");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        break;
                    case 8:
                        holder.setTextView(R.id.status, "整改完待验收");
                        holder.getView(R.id.status0).setVisibility(View.VISIBLE);
                        holder.getView(R.id.look_progress).setVisibility(View.VISIBLE);
                        holder.getView(R.id.jungong).setVisibility(View.GONE);
                        holder.getView(R.id.kaishi).setVisibility(View.GONE);
                        holder.getView(R.id.up_progress).setVisibility(View.GONE);
                        holder.getView(R.id.quxiao).setVisibility(View.GONE);
                        holder.getView(R.id.gotopay).setVisibility(View.GONE);
                        break;
                }

                RecyclerView shop = holder.getView(R.id.shop);
                shop.setLayoutManager(new LinearLayoutManager(ReceiveAllActivity.this, LinearLayoutManager.VERTICAL, false));
                shop.setAdapter(new BaseRVAdapter(ReceiveAllActivity.this, info1.get(position).getOrderdetail()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_order;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position2) {

                        ImageLoader.getInstance().displayImage( info1.get(position).getOrderdetail().get(position2).getGoods_img(),holder.getImageView(R.id.img));
                        holder.setTextView(R.id.title, info1.get(position).getOrderdetail().get(position2).getGoods_name());
                        holder.setTextView(R.id.type, info1.get(position).getOrderdetail().get(position2).getGoods_name());
                        holder.setTextView(R.id.area, info1.get(position).getOrderdetail().get(position2).getGoods_des());
                        holder.setTextView(R.id.num, "x" + info1.get(position).getOrderdetail().get(position2).getNums() + "/" + info1.get(position).getOrderdetail().get(position2).getUnit());
                        holder.setTextView(R.id.price, "￥" + info1.get(position).getOrderdetail().get(position2).getPrice());
                        holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // startActivity(new Intent(activity, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getId()).putExtra("isUp", 1));
                                getDetails(info1.get(position).getOrderid());

                            }
                        });
                    }
                });
                //查看进度
                holder.getView(R.id.look_progress).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(ReceiveAllActivity.this, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getOrderid()));
                    }
                });


                //详情
                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // startActivity(new Intent(activity, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getId()).putExtra("isUp", 1));
                        getDetails(info1.get(position).getOrderid());

                    }
                });
                //上传进度
                holder.getView(R.id.up_progress).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(ReceiveAllActivity.this, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getOrderid()).putExtra("isUp", 1));
                    }
                });
                //开始安装
                holder.getView(R.id.kaishi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // startActivity(new Intent(ReceiveAllActivity.this, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getId()).putExtra("isUp", 1));
                        showDialogv7("是否开始安装?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                upanzhuang(info1.get(position).getOrderid(), position);
                            }
                        });
                    }
                });
                //竣工
                holder.getView(R.id.jungong).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // startActivity(new Intent(ReceiveAllActivity.this, Wuliu2Activity.class).putExtra("budget_id", info1.get(position).getId()).putExtra("isUp", 1));
                        showDialogv7("您确定已完成安装吗?", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                junGong(info1.get(position).getOrderid(), position);
                            }
                        });
                    }
                });

            }
        };
        content.setAdapter(adapter);

    }

    private void junGong(String orderid, final int position) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_ENDDECORATE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        MyToast(info);
                        if (Integer.parseInt(info1.get(position).getStatus())==0){
                            info1.get(position).setStatus("5");
                        }else{
                            info1.remove(position);
                            if (info1.size() == 0) {
                                content.setVisibility(View.GONE);
                            } else {
                                content.setVisibility(View.VISIBLE);
                            }
                        }

                        adapter.notifyDataSetChanged();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });

    }

    private void upanzhuang(String position, final int pos) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", position);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_STARTDECORATE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        MyToast(info);
                        if (Integer.parseInt(info1.get(pos).getStatus())==0){
                            info1.get(pos).setStatus("4");
                        }else{
                            info1.remove(pos);
                            if (info1.size() == 0) {
                                content.setVisibility(View.GONE);
                            } else {
                                content.setVisibility(View.VISIBLE);
                            }
                        }



                        adapter.notifyDataSetChanged();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });

    }

    private void getDetails(String orderid) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", orderid);
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_DECORATEORDERDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        Gson gson = new Gson();
                        InstallDetailsBean bean = gson.fromJson(result, InstallDetailsBean.class);
                        InstallDetailsBean.InfoEntity info2 = bean.getInfo();
                        startActivity(new Intent(ReceiveAllActivity.this, InstallDetailsActivity.class).putExtra("info2", info2));
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }
}
